import AppHome from '@/views/AppHome/index.vue'
import AppList from '@/views/AppList/index.vue'
import AppLogin from '@/views/AppLogin/index.vue'
import AppSearch from '@/views/AppSearch/index.vue'
import AppNotFound from '@/views/NotFound/index.vue'
import AppHomeContainer from '@/views/AppHome/AppHomeContainer/index.vue'
import AppHomeBannerUp from '@/views/AppHome/AppHomeBanner/BannerUp/index.vue'
import AppHomebannerList from '@/views/AppHome/AppHomeBanner/BannerList/index.vue'
import AppAdmin from '@/views/AppAdmin/index.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    redirect: 'container',
    component: AppHome,
    children: [
      {
        path: 'container',
        name: 'container',
        component: AppHomeContainer,
        title: '数据首页'
      },
      {
        path: 'list',
        name: 'list',
        component: AppList,
        title: '商品列表'
      },
      {
        path: 'search',
        name: 'search',
        component: AppSearch,
        title: '商品搜索'
      },
      {
        path: 'admin',
        name: 'admin',
        component: AppAdmin,
        title: '管理员列表'
      },
      {
        path: '',
        name: 'banner',
        title: '轮播图管理'
      },
      {
        path: 'bannerup',
        name: 'bannerup',
        component: AppHomeBannerUp,
        title: '轮播图上传',
        parent: 'banner'
      },
      {
        path: 'bannerlist',
        name: 'bannerlist',
        component: AppHomebannerList,
        title: '热门商品轮播图',
        parent: 'banner'
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: AppLogin
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: AppNotFound
  }
]


// 配置渲染列表使用的路由列表数组
// 创建一个获取路由列表的方法
const newRouterDataGet = (items) => {
  // 创建一个用来接收数据的新数组
  const newArr = []
  for (let i = 0; i < items.length; i++) {
    // 判断当前循环的数据内有没有parent
    if (!items[i].parent) {
      // 没有就直接放进新数组内
      newArr.push(items[i])
    } else {
      // 有的话就在新数组中寻找有没有name跟自身parent的值一样的对象并使用find方法查找出来
      const parentArr = newArr.find(item => item.name === items[i].parent)
      // 判断该对象是否有kids内容
      // 有则直接放入已有的数组中
      if (parentArr) {
        if (parentArr.kids) {
          parentArr.kids.push(items[i])
        } else {
          // 没有则将该此循环的数据放入新数组kids中
          parentArr.kids = [items[i]]
        }
      }
    }
  }
  // 将新数组返回出去
  return newArr
}
export const newNavs = newRouterDataGet(routes[0].children)



export default routes